<template>
  <el-card class="Role-container">
    <template #header>
      <div class="header">
        <h3>课程订阅</h3>
      </div>
    </template>
    <h4>已订阅列表</h4>
    <el-table
        v-loading="order.loading"
        ref="multipleTable"
        title="已订阅列表"
        :data="order.tableData"
        tooltip-effect="dark"
        style="width: 100%">
      <el-table-column
          prop="name"
          label="课程名"
          width="400"
      >
      </el-table-column>
      <el-table-column
          prop="teacherName"
          label="课程老师"
      >
      </el-table-column>
      <el-table-column
          prop="major"
          label="专业"
      >
      </el-table-column>
      <el-table-column
          label="操作"
      >
        <template #default="scope">
          <a style="cursor: pointer; margin-right: 10px" @click="handleNoOrder(scope.row.id)">取消订阅</a>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination
        background
        layout="prev, pager, next"
        :total="order.total"
        :page-size="order.pageSize"
        :current-page="order.currentPage"
        @current-change="changeOrderPage"
    />
    <h4>未订阅列表</h4>
    <el-table
        v-loading="noOrder.loading"
        ref="multipleTable"
        title="未订阅列表"
        :data="noOrder.tableData"
        tooltip-effect="dark"
        style="width: 100%">
      <el-table-column
          prop="name"
          label="课程名"
          width="400"
      >
      </el-table-column>
      <el-table-column
          prop="teacherName"
          label="课程老师"
      >
      </el-table-column>
      <el-table-column
          prop="major"
          label="专业"
      >
      </el-table-column>
      <el-table-column
          label="操作"
      >
        <template #default="scope">
          <a style="cursor: pointer; margin-right: 10px" @click="handleOrder(scope.row.id)">订阅</a>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination
        background
        layout="prev, pager, next"
        :total="noOrder.total"
        :page-size="noOrder.pageSize"
        :current-page="noOrder.currentPage"
        @current-change="changeNoOrderPage"
    />
  </el-card>

</template>

<script>
import {onMounted, reactive, toRefs} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import moment from "moment";
import axios from "@/utils/axios";
import {ElMessage} from "element-plus";

export default {
  name: 'menu',
  setup() {
    const router = useRouter()
    const route = useRoute()
    const state = reactive({
      order: {
        loading: false,
        tableData: [], // 数据列表
        multipleSelection: [], // 选中项
        total: 0, // 总条数
        currentPage: 1, // 当前页
        pageSize: 10, // 分页大小
      },
      noOrder: {
        loading: false,
        tableData: [], // 数据列表
        multipleSelection: [], // 选中项
        total: 0, // 总条数
        currentPage: 1, // 当前页
        pageSize: 10, // 分页大小
      }
    })
    onMounted(() => {
      getInfo()
    })
    // 获取分类列表
    const getInfo = () => {
      state.order.loading = true
      axios.get('/evaluate/course/page/order', {
        params: {
          current: state.order.currentPage,
          size: state.order.pageSize,
        }
      }).then(res => {
        state.order.tableData = res.records
        state.order.total = res.total
        state.order.currentPage = res.current
        state.order.loading = false
      })
      state.noOrder.loading = true
      axios.get('/evaluate/course/page/noOrder', {
        params: {
          current: state.noOrder.currentPage,
          size: state.noOrder.pageSize,
        }
      }).then(res => {
        state.noOrder.tableData = res.records
        state.noOrder.total = res.total
        state.noOrder.currentPage = res.current
        state.noOrder.loading = false
      })
    }
    const changeOrderPage = (val) => {
      state.order.currentPage = val
      getInfo()
    }

    const changeNoOrderPage = (val) => {
      state.noOrder.currentPage = val
      getInfo()
    }

    const handleOrder = (id) => {
      axios.post("/evaluate/course/order", null, {
        params: {
          courseId: id
        }
      }).then(() => {
        ElMessage.success("订阅成功")
        getInfo()
      })
    }

    const handleNoOrder = (id) => {
      axios.delete("/evaluate/course/order",  {
        params: {
          courseId: id
        }
      }).then(() => {
        ElMessage.success("删除订阅成功")
        getInfo()
      })
    }

    return {
      ...toRefs(state),
      changeOrderPage,
      changeNoOrderPage,
      handleOrder,
      handleNoOrder,
      dateFormat: function (row, column) {
        const date = row[column.property];
        if (date === undefined) {
          return ''
        }
        return moment(date).format("YYYY-MM-DD HH:mm:ss")
      }
    }
  }
}
</script>

<style scoped>
.Role-container {
  min-height: 100%;
}

.el-card.is-always-shadow {
  min-height: 100% !important;
}
</style>